package com.iss.apartmentmanage.controller.systemSetting;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iss.apartmentmanage.config.menu.MenuTreeUtils;
import com.iss.apartmentmanage.entity.Menu;
import com.iss.apartmentmanage.entity.Role;
import com.iss.apartmentmanage.mapper.MenuMapper;
import com.iss.apartmentmanage.model.MenuWithRole;
import com.iss.apartmentmanage.model.MyResponseData;
import com.iss.apartmentmanage.service.RoleService;
import com.iss.apartmentmanage.utils.MyJson;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
@Api(description = "角色管理模块接口",value = "RoleController")
@RequestMapping("/admin/role")
public class RoleController {
    /**
     * 分页查询全部角色
     * @return
     */
    @Autowired
    RoleService roleService;
    @Autowired
    MenuMapper menuMapper;

    @PostMapping(value = "/queryRoles")
    public String queryRoles(@RequestParam int pageNum, @RequestParam int pageSize){
        MyResponseData myResponseData = new MyResponseData();
        IPage<Role> role=roleService.queryRoles(pageNum,pageSize);
        if (role!=null){
            myResponseData.setObject(role);
            myResponseData.setErrorCode(1);
        }
        return MyJson.getJson(myResponseData);
    }

    /**
     * 更新角色
     * @param role
     * @return
     */
    @PostMapping(value = "/updateRole")
    public String updateRole(@RequestBody Role role){
        MyResponseData myResponseData = new MyResponseData();
      Integer count=  roleService.updateRole(role);
        if (count!=0){
            myResponseData.setErrorCode(1);
            myResponseData.setMsg("成功");
        }
        return MyJson.getJson(myResponseData);
    }
    /**
     * 删除角色
     * @param id
     * @return
     */
    @GetMapping(value = "/deleteRole")
    public String deleteRole(@RequestParam Integer id){
        MyResponseData myResponseData = new MyResponseData();
        Integer count=  roleService.deleteRole(id);
        if (count!=0){
            myResponseData.setErrorCode(1);
            myResponseData.setMsg("成功");
        }
        return MyJson.getJson(myResponseData);
    }
    /**
     * 新加角色
     * @param role
     * @return
     */
    @PostMapping(value = "/addRole")
    public String addRole(@RequestBody Role role){
        MyResponseData myResponseData = new MyResponseData();
        Integer count=  roleService.addRole(role);
        if (count!=0){
            myResponseData.setErrorCode(1);
            myResponseData.setMsg("成功");
        }
        return MyJson.getJson(myResponseData);
    }
    /**
     * 菜单树的制作
     */
    @PostMapping(value = "/getMenuTree")
    public String getMenuTree( ){
        MyResponseData responseBody = new MyResponseData();
        List<Menu> menuList= menuMapper.selectMenu();
        Map<String,Object> data = new HashMap<>();
        data.put("menuTree", MenuTreeUtils.generateMenuTree(menuList));
        responseBody.setMsg("成功");
        responseBody.setErrorCode(1);
        responseBody.setObject(data);
        return MyJson.getJson(responseBody);
    }

    /**
     *添加role和menu的关系
     */
    @PostMapping(value = "/addRoleMenu")
    public String addRoleMenu(@RequestBody MenuWithRole menuWithRole){
        MyResponseData responseBody = new MyResponseData();
        int count =roleService.addRoleMenu(menuWithRole);
        if (count!=0){
            responseBody.setMsg("成功");
            responseBody.setErrorCode(1);
        }
        return MyJson.getJson(responseBody);
    }










}
